package com.huitone.gddw.controller.worktable;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.GateSwtich;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IGateSwtichService;
import com.huitone.gddw.service.dto.GateSwtichDTO;
//import com.huitone.gddw.utils.MD5Util;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信网关双线切换日志 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="gateSwtich")
public class GateSwtichController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(GateSwtichController.class);
	
	@Autowired
	private IGateSwtichService gateSwtichService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<GateSwtichDTO> query(QueryPage<GateSwtich> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询短信网关双线切换日志";
		QueryPage<GateSwtichDTO> result = null;
		try {
			result = gateSwtichService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询短信网关双线切换日志信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 更改线路类型
	 * @param recId 记录ID
	 * @param lineType 更改后线路类型
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeLineType")
	public Message changeLineType(String recId, String lineType, String pwd) {
		try {
//			pwd = MD5Util.encode(pwd);
			if (!pwd.equals(SysLoginUtil.getLoginUser().getPwd())) {
				return new Message(false, "密码不正确！");
			}
			gateSwtichService.changeLineType(recId, lineType);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改线路类型过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	@ResponseBody
	@RequestMapping(value="autoChangeLine")
	public Message autoChangeLine(String status,String pwd) {
		try {
//			pwd = MD5Util.encode(pwd);
			if (!pwd.equals(SysLoginUtil.getLoginUser().getPwd())) {
				return new Message(false, "密码不正确！");
			}
			gateSwtichService.autoChangeSwitch(status);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("手动自动切换失败：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
}
